package com.example.workspace.douying;


import com.example.workspace.dto.PositionDto;
import com.example.workspace.utils.MatchImage;
import com.example.workspace.utils.MouseUtils;
import com.example.workspace.utils.ToolUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;


public class Follow {

    private static final String FOLLOW_TEMPLATE = "D:\\image\\follow_template.png";

    public static void main(String[] args) {

       //Robot截图
        try {
            Robot robot = new Robot();


            ToolUtils.loadOpencv();


            for (int i = 0; i < 2; i++) {
                //当前电脑截全屏图片,作为原图分析
                Dimension screensize=Toolkit.getDefaultToolkit().getScreenSize();
                BufferedImage bufferedimage=robot.createScreenCapture(new Rectangle(0,0,screensize.width,screensize.height));
                String srcImg = "D:\\image\\temp\\" + System.currentTimeMillis() + ".png";
                File f=new File(srcImg);
                OutputStream os = new FileOutputStream(f);
                ImageIO.write(bufferedimage, "png", os);
                os.flush();
                os.close();

                //得到坐标数据
                List<PositionDto> matchImagePosition = MatchImage.matchImage(srcImg, FOLLOW_TEMPLATE);
                MouseUtils.mouseMoveAndClick(robot,matchImagePosition,3);

                f.delete();
                //鼠标滚轮
                robot.mouseWheel(10);
                Thread.sleep(5000);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }

}
